﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("OpenPhotoAlbum.Controls");

OpenPhotoAlbum.Controls.TooltipBehavior = function(element) {
    OpenPhotoAlbum.Controls.TooltipBehavior.initializeBase(this, [element]);

    this._Text = null;
    this._Position = null; // 0:Top.1:Right.2:Bottom.3:Left.
    this._ParentElementId = null;

    this._tooltipElement = null;
    this._enabled = true;
}

OpenPhotoAlbum.Controls.TooltipBehavior.prototype = {
	initialize: function() {
		OpenPhotoAlbum.Controls.TooltipBehavior.callBaseMethod(this, 'initialize');

		this._tooltipElement = document.createElement('div');
		this._tooltipElement.className = 'tooltip';
		this._tooltipElement.innerHTML = this._Text;
		$get(this._ParentElementId).appendChild(this._tooltipElement);

		this._hideTooltip();

		$addHandler(this._element, "mouseover", Function.createDelegate(this, this.onMouseOver));
		$addHandler(this._element, "mouseout", Function.createDelegate(this, this.onMouseOut));
	},
	dispose: function() {
		$clearHandlers(this._element);

		OpenPhotoAlbum.Controls.TooltipBehavior.callBaseMethod(this, 'dispose');
	},

	_setPosition: function() {
		//var contentBounds = Sys.UI.DomElement.getBounds(this._element);
		//var tooltipBounds = Sys.UI.DomElement.getBounds(this._tooltipElement);
		var contentOffset = $(this._element).offset();
		var contentWidth = $(this._element).width();
		var contentHeight = $(this._element).height();
		var tooltipOffset = $(this._tooltipElement).offset();
		var tooltipWidth = $(this._tooltipElement).width();
		var tooltipHeight = $(this._tooltipElement).height();
		var x, y;
		if (this._Position == 0) { x = contentOffset.left - 1; y = contentOffset.top - tooltipHeight - 1; }
		else if (this._Position == 1) { x = contentOffset.left + contentWidth + 1; y = contentOffset.top; }
		else if (this._Position == 2) { x = contentOffset.left - 1; y = contentOffset.top + contentHeight + 1; }
		else if (this._Position == 3) { x = contentOffset.left - tooltipWidth - 1; y = contentOffset.top; }
		//Sys.UI.DomElement.setLocation(this._tooltipElement, x, y);
		$(this._tooltipElement).css({ left: x, top: y });
	},

	_hideTooltip: function() {
		$(this._tooltipElement).css({ visibility: 'hidden', position: 'absolute', x: 0, y: 0 });
	},

	_showTooltip: function() {
		//this._tooltipElement.style.display = '';
		$(this._tooltipElement).css({ visibility: 'visible' });
		this._setPosition();
	},

	onMouseOver: function(e) {
		if (this._enabled) { this._showTooltip(); }
	},

	onMouseOut: function(e) {
		this._hideTooltip();
	},

	enableTooltip: function() {
		this._enabled = true;
	},

	disableTooltip: function() {
		this._enabled = false;
		this._hideTooltip();
	},

	get_Text: function() { return this._Text; },
	set_Text: function(value) { this._Text = value; },

	get_Position: function() { return this._Position; },
	set_Position: function(value) { this._Position = value; },

	get_ParentElementId: function() { return this._ParentElementId; },
	set_ParentElementId: function(value) { this._ParentElementId = value; }
}
OpenPhotoAlbum.Controls.TooltipBehavior.registerClass('OpenPhotoAlbum.Controls.TooltipBehavior', AjaxControlToolkit.BehaviorBase);
